home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 July: Mac OS SDK / Dev.CD Jul 00 SDK2.toast / Development Kits / Cross Platform / QuickTime 4.1.2 Windows SDK / CIncludes / OpenTransportKernel.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-12  |  38.8 KB  |  1,532 lines  |  [TEXT/R*ch]

  1. /*
  2.      File:        OpenTransportKernel.h
  3.  
  4.      Contains:    Definitions for Open Transport kernel code, such as drivers and protocol modules.
  5.  
  6.      Version:    Technology:    2.5
  7.                  Release:    QuickTime 4.1
  8.  
  9.      Copyright:    (c) 1993-1999 by Apple Computer, Inc. and Mentat Inc., all rights reserved.
  10.  
  11.      Bugs?:        For bug reports, consult the following page on
  12.                  the World Wide Web:
  13.  
  14.                      http://developer.apple.com/bugreporter/
  15.  
  16. */
  17. #ifndef __OPENTRANSPORTKERNEL__
  18. #define __OPENTRANSPORTKERNEL__
  19.  
  20. #ifdef OTKERNEL
  21. #if !OTKERNEL
  22. #error OpenTransportModule.h can only be used by kernel code.
  23. #endif  /* !OTKERNEL */
  24.  
  25. #endif  /* defined(OTKERNEL) */
  26.  
  27. #define OTKERNEL 1
  28. /*
  29.    We include "OpenTransportProtocol.h", which in turn includes
  30.    "OpenTransport.h", thereby picking up all the stuff which
  31.    is shared between client and kernel.
  32. */
  33. #ifndef __OPENTRANSPORTPROTOCOL__
  34.     #include <OpenTransportProtocol.h>
  35. #endif
  36.  
  37. #ifndef __NAMEREGISTRY__
  38.     #include <NameRegistry.h>
  39. #endif
  40.  
  41.  
  42.  
  43.  
  44. #if PRAGMA_ONCE
  45. #pragma once
  46. #endif
  47.  
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51.  
  52. #if PRAGMA_IMPORT
  53. #pragma import on
  54. #endif
  55.  
  56. #if PRAGMA_STRUCT_ALIGN
  57.     #pragma options align=mac68k
  58. #elif PRAGMA_STRUCT_PACKPUSH
  59.     #pragma pack(push, 2)
  60. #elif PRAGMA_STRUCT_PACK
  61.     #pragma pack(2)
  62. #endif
  63.  
  64.  
  65. #if defined(__MWERKS__) && TARGET_CPU_68K
  66.     #pragma push
  67.     #pragma pointers_in_D0
  68. #endif
  69.  
  70. /* ***** From the Mentat "mistream.h" ******/
  71.  
  72.  
  73. /*
  74.    Parts of "mistream.h" that are shared by the client
  75.    and the kernel are in "OpenTransportProtocol.h".
  76. */
  77.  
  78.  
  79.  
  80. /** Re-allow a queue to be scheduled for service */
  81. #define enableok(q)     ((q)->q_flag &= ~QNOENB)
  82.  
  83. /** Prevent a queue from being scheduled */
  84. #define noenable(q)     ((q)->q_flag |= QNOENB)
  85.  
  86. /** Get pointer to the mate queue */
  87. #define OTHERQ(q)        (((q)->q_flag & QREADR) ? WR(q) : RD(q))
  88.  
  89. /** Get pointer to the read queue, assumes 'q' is a write queue ptr */
  90. #define RD(q)    (&q[-1])
  91.  
  92. /** Get pointer to the write queue, assumes 'q' is a read queue ptr */
  93. #define WR(q)    (&q[1])
  94.  
  95. EXTERN_API_C( mblk_t *)
  96. allocb                            (OTByteCount             size,
  97.                                  OTInt32                 pri);
  98.  
  99. typedef CALLBACK_API_C( void , allocbiProc )(char *arg);
  100. EXTERN_API_C( mblk_t *)
  101. allocbi                            (OTByteCount             size,
  102.                                  OTInt32                 pri,
  103.                                  allocbiProc             pfv,
  104.                                  char *                    arg,
  105.                                  unsigned char *        base);
  106.  
  107. EXTERN_API_C( queue_t *)
  108. allocq                            (void);
  109.  
  110. EXTERN_API_C( OTInt32 )
  111. adjmsg                            (mblk_t *                mp,
  112.                                  OTInt32                 len_param);
  113.  
  114. EXTERN_API_C( queue_t *)
  115. backq                            (queue_t *                q);
  116.  
  117. EXTERN_API_C( OTInt32 )
  118. bcanput                            (queue_t *                q,
  119.                                  OTUInt8Param             pri);
  120.  
  121. EXTERN_API_C( OTInt32 )
  122. bcanputnext                        (queue_t *                q,
  123.                                  OTUInt8Param             pri);
  124.  
  125. EXTERN_API_C( OTInt32 )
  126. bufcall                            (OTByteCount             memNeeded,
  127.                                  OTInt32                 pri,
  128.                                  bufcallp_t             proc,
  129.                                  long                     context);
  130.  
  131. EXTERN_API_C( OTInt32 )
  132. canput                            (queue_t *                q);
  133.  
  134. EXTERN_API_C( OTInt32 )
  135. canputnext                        (queue_t *                q);
  136.  
  137. EXTERN_API_C( mblk_t *)
  138. copyb                            (mblk_t *                mp);
  139.  
  140. EXTERN_API_C( mblk_t *)
  141. copymsg                            (mblk_t *                mp);
  142.  
  143. EXTERN_API_C( mblk_t *)
  144. dupb                            (mblk_t *                mp);
  145.  
  146. EXTERN_API_C( mblk_t *)
  147. dupmsg                            (mblk_t *                mp);
  148.  
  149. EXTERN_API_C( mblk_t *)
  150. esballoc                        (unsigned char *        base,
  151.                                  OTByteCount             size,
  152.                                  OTInt32                 pri,
  153.                                  frtn_t *                freeInfo);
  154.  
  155. EXTERN_API_C( mblk_t *)
  156. esballoca                        (unsigned char *        base,
  157.                                  OTByteCount             size,
  158.                                  OTInt32                 pri,
  159.                                  frtn_t *                freeInfo);
  160.  
  161. typedef CALLBACK_API_C( void , esbbcallProc )(long arg);
  162. EXTERN_API_C( OTInt32 )
  163. esbbcall                        (OTInt32                 pri,
  164.                                  esbbcallProc             func,
  165.                                  long                     arg);
  166.  
  167. EXTERN_API_C( void )
  168. flushband                        (queue_t *                q,
  169.                                  OTUInt8Param             pri,
  170.                                  OTInt32                 flag);
  171.  
  172. EXTERN_API_C( void )
  173. flushq                            (queue_t *                q,
  174.                                  OTInt32                 flag);
  175.  
  176. EXTERN_API_C( void )
  177. freeb                            (mblk_t *                mp);
  178.  
  179. EXTERN_API_C( void )
  180. freemsg                            (mblk_t *                mp);
  181.  
  182. EXTERN_API_C( OTInt32 )
  183. freeq                            (queue_t *                q);
  184.  
  185. EXTERN_API_C( OTInt32 )
  186. freezestr                        (queue_t *                q);
  187.  
  188. EXTERN_API_C( admin_t )
  189. getadmin                        (OTUInt16Param             mid);
  190.  
  191. EXTERN_API_C( unsigned short )
  192. getmid                            (char *                    name);
  193.  
  194. EXTERN_API_C( mblk_t *)
  195. getq                            (queue_t *                q);
  196.  
  197. EXTERN_API_C( OTInt32 )
  198. insq                            (queue_t *                q,
  199.                                  mblk_t *                emp,
  200.                                  mblk_t *                nmp);
  201.  
  202. EXTERN_API_C( void )
  203. linkb                            (mblk_t *                mp1,
  204.                                  mblk_t *                mp2);
  205.  
  206. typedef CALLBACK_API_C( void , mpnotifyProc )(char *arg);
  207. EXTERN_API_C( OTInt32 )
  208. mpnotify                        (mblk_t *                mp,
  209.                                  mpnotifyProc             pfv,
  210.                                  char *                    arg);
  211.  
  212. EXTERN_API_C( OTInt32 )
  213. msgdsize                        (const mblk_t *            mp);
  214.  
  215. EXTERN_API_C( mblk_t *)
  216. msgpullup                        (mblk_t *                mp,
  217.                                  OTInt32                 len);
  218.  
  219. EXTERN_API_C( OTInt32 )
  220. pullupmsg                        (mblk_t *                mp,
  221.                                  OTInt32                 len);
  222.  
  223. EXTERN_API_C( OTInt32 )
  224. put                                (queue_t *                q,
  225.                                  mblk_t *                mp);
  226.  
  227. EXTERN_API_C( OTInt32 )
  228. putbq                            (queue_t *                q,
  229.                                  mblk_t *                mp);
  230.  
  231. EXTERN_API_C( OTInt32 )
  232. putctl                            (queue_t *                q,
  233.                                  OTInt32                 mType);
  234.  
  235. EXTERN_API_C( OTInt32 )
  236. putnextctl                        (queue_t *                q,
  237.                                  OTInt32                 mType);
  238.  
  239. EXTERN_API_C( OTInt32 )
  240. putctl1                            (queue_t *                q,
  241.                                  OTInt32                 mType,
  242.                                  OTInt32                 c);
  243.  
  244. EXTERN_API_C( OTInt32 )
  245. putnextctl1                        (queue_t *                q,
  246.                                  OTInt32                 mType,
  247.                                  OTInt32                 c);
  248.  
  249. EXTERN_API_C( OTInt32 )
  250. putctl2                            (queue_t *                q,
  251.                                  OTInt32                 mType,
  252.                                  OTInt32                 c1,
  253.                                  OTInt32                 c2);
  254.  
  255. #ifndef puthere
  256. EXTERN_API_C( OTInt32 )
  257. puthere                            (queue_t *                q,
  258.                                  mblk_t *                mp);
  259.  
  260. #endif  /* !defined(puthere) */
  261.  
  262. EXTERN_API_C( OTInt32 )
  263. putnext                            (queue_t *                q,
  264.                                  mblk_t *                mp);
  265.  
  266. EXTERN_API_C( OTInt32 )
  267. putq                            (queue_t *                q,
  268.                                  mblk_t *                mp);
  269.  
  270. EXTERN_API_C( void )
  271. qenable                            (queue_t *                q);
  272.  
  273. EXTERN_API_C( void )
  274. qprocson                        (queue_t *                q);
  275.  
  276. EXTERN_API_C( void )
  277. qprocsoff                        (queue_t *                q);
  278.  
  279. EXTERN_API_C( OTInt32 )
  280. qreply                            (queue_t *                q,
  281.                                  mblk_t *                mp);
  282.  
  283. EXTERN_API_C( OTInt32 )
  284. qsize                            (queue_t *                q);
  285.  
  286. EXTERN_API_C( mblk_t *)
  287. rmvb                            (mblk_t *                mp,
  288.                                  mblk_t *                bp);
  289.  
  290. EXTERN_API_C( void )
  291. rmvq                            (queue_t *                q,
  292.                                  mblk_t *                mp);
  293.  
  294. /* prototype for strlog in "strlog.h" section, below */
  295. EXTERN_API_C( OTInt32 )
  296. strqget                            (queue_t *                q,
  297.                                  qfields_t                 what,
  298.                                  OTUInt8Param             pri,
  299.                                  long *                    valp);
  300.  
  301. EXTERN_API_C( OTInt32 )
  302. strqset                            (queue_t *                q,
  303.                                  qfields_t                 what,
  304.                                  OTUInt8Param             pri,
  305.                                  long                     val);
  306.  
  307. EXTERN_API_C( OTInt32 )
  308. testb                            (OTByteCount             size,
  309.                                  OTInt32                 pri);
  310.  
  311. EXTERN_API_C( void )
  312. unbufcall                        (OTInt32                 id);
  313.  
  314. EXTERN_API_C( void )
  315. unfreezestr                        (queue_t *                q,
  316.                                  OTInt32                 oldpri);
  317.  
  318. EXTERN_API_C( mblk_t *)
  319. unlinkb                            (mblk_t *                mp);
  320.  
  321. /* ***** From the Mentat "strlog.h" ******/
  322.  
  323.  
  324. EXTERN_API_C( OTInt32 )
  325. strlog                            (OTInt32                 mid,
  326.                                  OTInt32                 sid,
  327.                                  OTInt32                 level,
  328.                                  OTUInt32                 flags,
  329.                                  char *                    fmt,
  330.                                  ...);
  331.  
  332. /* ***** Printing Functions ******/
  333.  
  334. enum {
  335.     kOTPrintOnly                = 0,
  336.     kOTPrintThenStop            = 1
  337. };
  338.  
  339. EXTERN_API_C( OTInt32 )
  340. OTKernelPrintf                    (OTInt32                 toDo,
  341.                                  char *                    fmt,
  342.                                  ...);
  343.  
  344. enum {
  345.     CE_CONT                        = 0,                            /* Does kOTPrintOnly        */
  346.     CE_NOTE                        = 0,                            /* Just print */
  347.     CE_WARN                        = 1,                            /* Does kOTPrintThenStop    */
  348.     CE_PANIC                    = 2                                /* Does System Error 107    */
  349. };
  350.  
  351. EXTERN_API_C( void )
  352. cmn_err                            (OTInt32                 errType,
  353.                                  char *                    fmt,
  354.                                  ...);
  355.  
  356. EXTERN_API_C( OTInt32 )
  357. mi_sprintf                        (char *                    buf,
  358.                                  char *                    fmt,
  359.                                  ...);
  360.  
  361. /* Create sprintf and printf functions that will work in STREAM modules.*/
  362.  
  363.  
  364. #define sprintf                        mi_sprintf
  365. #define printf                        OTKernelPrintf
  366.  
  367. /* ***** FIIK ******/
  368.  
  369. /* ooo useful header comment please ooo*/
  370.  
  371. /*
  372.    Function to convert the "long" value that comes back in some of the
  373.    netbufs as a result code to the equivalent OSStatus
  374. */
  375.  
  376. typedef long                             OTError;
  377.  
  378. #define GetEError(v)            ((OTUnixErr)(((v) >> 16) & 0xffff))
  379. #define GetXTIError(v)            ((OTXTIErr)((v) & 0xffff))
  380. #define MakeTPIEError(e)        ((OTError)(((((UInt16)(e)) << 16) | TSYSERR)))
  381. #define MakeDLPIEError(e)        ((OTError)(((((UInt16)(e)) << 16) | DL_SYSERR)))
  382. #define MakeXTIError(xti)        ((OTError)(xti))
  383. #define MakeOTError(xti, e)        ((OTError)((xti) | ((UInt16)(e)) << 16))
  384.  
  385. EXTERN_API_C( OSStatus )
  386. OTErrorToOSStatus                (OTError                 err);
  387.  
  388. /* ***** STREAMS Plug-in Interface *****/
  389.  
  390. /*
  391.    Synchronization level codes.  These are supplied to modsw_install and
  392.    stored in the appropriate tables.  sth_osr_open and
  393.    sth_ipush use these to set up synch queue subordination for new devices
  394.    and modules.
  395. */
  396.  
  397. enum {
  398.     SQLVL_QUEUE                    = 1,
  399.     SQLVL_QUEUEPAIR                = 2,
  400.     SQLVL_MODULE                = 3,
  401.     SQLVL_GLOBAL                = 4,
  402.     SQLVL_DEFAULT                = 3
  403. };
  404.  
  405. /* The install_info structure.*/
  406.  
  407.  
  408. struct install_info {
  409.     streamtab *                        install_str;                /* Streamtab pointer.        */
  410.     UInt32                             install_flags;
  411.     UInt32                             install_sqlvl;                /* Synchronization level.    */
  412.     char *                            install_buddy;                /* Shared writer list buddy */
  413.     long                             ref_load;                    /* Set to 0                    */
  414.     UInt32                             ref_count;                    /* set to 0                    */
  415. };
  416. typedef struct install_info                install_info;
  417. /*    Flags used in the install_flags field*/
  418. enum {
  419.     kOTModIsDriver                = 0x00000001,
  420.     kOTModIsModule                = 0x00000002,
  421.     kOTModNoWriter                = 0x00000010,
  422.     kOTModUpperIsTPI            = 0x00001000,
  423.     kOTModUpperIsDLPI            = 0x00002000,
  424.     kOTModLowerIsTPI            = 0x00004000,
  425.     kOTModLowerIsDLPI            = 0x00008000,
  426.     kOTModGlobalContext            = 0x00800000,                    /* This flag says you don't want per-context globals*/
  427.     kOTModUsesInterrupts        = 0x08000000,                    /* This flag is only valid if kOTModIsDriver is set and the driver is a PCI-card driver using the Name Registry*/
  428.     kOTModIsComplexDriver        = 0x20000000,                    /* This flag is only valid if kOTModIsDriver is set.*/
  429.     kOTModIsFilter                = 0x40000000                    /* This flag is only valid if kOTModIsModule is set.*/
  430. };
  431.  
  432. /* Typedef for the GetOTInstallInfo function*/
  433.  
  434. /*
  435.    Your module must export this function, and return a pointer to the
  436.    install_info structure for the module.
  437. */
  438.  
  439. typedef CALLBACK_API_C( install_info *, GetOTInstallInfoProcPtr )(void );
  440. /* Typedef for the InitStreamModule function*/
  441. /*
  442.    Your module can optionally export this function.  It will be called 
  443.    whenever your module is about to be loaded into a stream for the
  444.    first time, or if it is about to be reloaded after having been 
  445.    unloaded. Return false if your module should NOT be loaded.
  446.    For STREAMS modules, the void* parameter will be NULL.  For drivers, it
  447.    will be the same cookie parameter that was used for registering the module.
  448.    For PCI card drivers, this will be a pointer to the OTPCIInfo structure,
  449.    which can also be interpreted as a RegEntryIDPtr.
  450. */
  451.  
  452. typedef CALLBACK_API_C( Boolean , InitStreamModuleProcPtr )(void *portInfo);
  453. /* Typedef for the TerminateStreamModule function*/
  454. /*
  455.    Your module can optionally export this function.  It will be called 
  456.    whenever your module has been closed for the last time (i.e. no other 
  457.    outstanding instances of the module exist).
  458. */
  459.  
  460. typedef CALLBACK_API_C( void , TerminateStreamModuleProcPtr )(void *portInfo);
  461. /* Equates for shared library prefixes*/
  462.     
  463.     /*
  464.      * Interface ID for STREAMS Modules for ASLM.
  465.     */
  466. #define kOTModuleInterfaceID        kOTModulePrefix "StrmMod"
  467.  
  468.  
  469. /* ***** Majors and Minors ******/
  470.  
  471. /* Functions for dealing with major and minor device numbers*/
  472.  
  473.  
  474. typedef UInt16                             major_t;
  475. typedef UInt16                             minor_t;
  476.  
  477. /* major part of a device */
  478. #define getmajor(x)         ((major_t)(((unsigned long)(x)>>16)&0xffff))
  479.  
  480. /* minor part of a device */
  481. #define getminor(x)         ((minor_t)((x)&0xffff))
  482.  
  483. /* make a device number */
  484. #define makedev(x,y)        ((dev_t)((((dev_t)(x))<<16) | ((y) & 0xffff)))
  485.  
  486. #define getemajor            getmajor
  487. #define geteminor            getminor
  488. #define makedevice            makedev
  489.  
  490. #define    etoimajor(majnum)    (majnum)
  491. #define    itoemajor(majnum,j)    (majnum)
  492.  
  493. /*
  494.    This is the first minor number that Apple OT drivers use for CLONEOPENs.
  495.    Minor numbers 0 through 9 are reserved for use by the modules
  496.    for various control streams.  Note that Mentat drivers, which
  497.    use mi_open_comm, start minor numbers from 5.
  498. */
  499. enum {
  500.     kFirstMinorNumber            = 10
  501. };
  502.  
  503. /* ***** Logging Macros ******/
  504.  
  505. EXTERN_API_C( void )
  506. OTStrlog                        (queue_t *                q,
  507.                                  OTInt32                 lvl,
  508.                                  OTInt32                 flags,
  509.                                  const char *            str);
  510.  
  511. /* These are enums for the level value*/
  512.  
  513. enum {
  514.     kOTLvlFatal                    = 0,
  515.     kOTLvlNonfatal                = 1,
  516.     kOTLvlExtFatal                = 2,
  517.     kOTLvlExtNonfatal            = 3,
  518.     kOTLvlUserErr                = 4,
  519.     kOTLvlInfoErr                = 5,
  520.     kOTLvlInfoOnly                = 6
  521. };
  522.  
  523.  
  524.  
  525. #define STRLOG(q, lvl, flags, str)        OTStrlog(q, lvl, flags, str)
  526.  
  527. #if OTDEBUG
  528.     #define STRLOG1(q, lvl, flags, str)    OTStrlog(q, lvl, flags, str)
  529. #else
  530.     #define STRLOG1(q, lvl, flags, str)
  531. #endif
  532.  
  533. #if OTDEBUG > 1 || OTDEBUG > 1
  534.     #define STRLOG2(q, lvl, flags, str)    OTStrlog(q, lvl, flags, str)
  535. #else
  536.     #define STRLOG2(q, lvl, flags, str)
  537. #endif
  538.  
  539. /* ***** TPI Additions ******/
  540.  
  541. /*
  542.    Extra OTCommand codes that may appear on your module queue.
  543.    These are extensions to the TPI specification for Open Transport.
  544.    T_PRIVATE_REQ is the first available TPI message number for private
  545.    use by modules (assuming you don't want to be confused by standard
  546.    TPI messages).
  547. */
  548.  
  549.  
  550.  
  551. enum {
  552.     T_TIMER_REQ        = 80,    /* Timer event                            */
  553.     T_MIB_REQ        = 81,    /* Request module's MIB                    */
  554.     T_MIB_ACK        = 82,    /* The module's MIB is available        */
  555.     
  556.     T_PRIVATE_REQ    = 90    /* The first private request available    */
  557. };
  558.  
  559.  
  560. struct T_MIB_req {
  561.     long                             PRIM_type;                    /* Always T_MIB_REQ */
  562. };
  563. typedef struct T_MIB_req                T_MIB_req;
  564.  
  565. struct T_MIB_ack {
  566.     long                             PRIM_type;                    /* Always T_MIB_ACK     */
  567.     long                             MIB_length;                    /* MIB length             */
  568.     long                             MIB_offset;                    /* MIB Offset            */
  569. };
  570. typedef struct T_MIB_ack                T_MIB_ack;
  571.  
  572. struct T_stream_timer {
  573.     long                             PRIM_type;                    /* Always T_TIMER_REQ */
  574.     union {
  575.         long                             USER_long;
  576.         void *                            USER_ptr;
  577.     }                                 USER_data;
  578. };
  579. typedef struct T_stream_timer            T_stream_timer;
  580. /* ***** Kernel Port Stuff ******/
  581. /*
  582.    Kernel port record, which is a direct analogue of
  583.    the OTPortRecord in "OpenTransport.h".  Note that
  584.    when working with TPortRecord's, you're always
  585.    working with OT's one true copy of the record,
  586.    whereas when working with OTPortRecord's, you're
  587.    always working with a copy.
  588. */
  589.  
  590.  
  591. struct TPortRecord {
  592.     OTLink                             fLink;
  593.     char *                            fPortName;
  594.     char *                            fModuleName;
  595.     char *                            fResourceInfo;
  596.     char *                            fSlotID;
  597.     struct TPortRecord *            fAlias;
  598.     ItemCount                         fNumChildren;
  599.     OTPortRef *                        fChildPorts;
  600.     UInt32                             fPortFlags;
  601.     UInt32                             fInfoFlags;
  602.     UInt32                             fCapabilities;
  603.     OTPortRef                         fRef;
  604.     streamtab *                        fStreamtab;
  605.     void *                            fContext;
  606.     void *                            fExtra;
  607. };
  608. typedef struct TPortRecord                TPortRecord;
  609. /* Port utilities*/
  610. /*
  611.    These routines can be used by kernel code to register, find and iterate
  612.    through the various ports on the machine.  Do not confuse these with
  613.    the client-side routines, defined in "OpenTransport.h".
  614. */
  615.  
  616. /*
  617.    Register a port. The name the port was registered under is returned in
  618.    the fPortName field.  This routine allocates a TPortRecord and
  619.    copies the supplied OTPortRecord into it.
  620. */
  621. EXTERN_API_C( OSStatus )
  622. OTRegisterPort                    (OTPortRecord *            portInfo,
  623.                                  void *                    ref);
  624.  
  625. /*
  626.    Unregister the port with the given name (If you re-register the
  627.    port, it may get a different name - use OTChangePortState if
  628.    that is not desireable).  Since a single OTPortRef can be registered
  629.    with several names, the API needs to use the portName rather than
  630.    the OTPortRef to disambiguate.
  631. */
  632. EXTERN_API_C( OSStatus )
  633. OTUnregisterPort                (const char *            portName,
  634.                                  void **                refPtr);
  635.  
  636. /* Change the state of the port.*/
  637. EXTERN_API_C( OSStatus )
  638. OTChangePortState                (OTPortRef                 portRef,
  639.                                  OTEventCode             theChange,
  640.                                  OTResult                 why);
  641.  
  642. /* Find the TPortRecord for a given Port Name*/
  643. EXTERN_API_C( TPortRecord *)
  644. OTFindPort                        (const char *            portName);
  645.  
  646. /* Find the "nth" TPortRecord*/
  647. EXTERN_API_C( TPortRecord *)
  648. OTGetIndexedPort                (OTItemCount             index);
  649.  
  650. /*
  651.    Find another port that is active and conflicts with
  652.    the port described by "ref"
  653. */
  654. EXTERN_API_C( TPortRecord *)
  655. OTFindPortConflict                (OTPortRef                 ref);
  656.  
  657. /* Other ways of finding the port*/
  658. EXTERN_API_C( TPortRecord *)
  659. OTFindPortByRef                    (OTPortRef                 ref);
  660.  
  661. EXTERN_API_C( TPortRecord *)
  662. OTFindPortByDev                    (dev_t                     dev);
  663.  
  664. /* ***** Port Scanners ******/
  665.  
  666. /* Shared library definitions*/
  667.  
  668. /*
  669.    Prefix for Open Transport port scanners.
  670.    Your port-scanning ASLM function set must use the prefix kOTPortScannerPrefix.
  671. */
  672.  
  673. #define kOTPortScannerPrefix        "ot:pScnr$"
  674. /*
  675.    The kOTPortScannerInterfaceID define is what you need to add to your
  676.    export file for the "interfaceID = " clause.  
  677. */
  678.  
  679.  
  680. #define kOTPortScannerInterfaceID            kOTKernelPrefix "pScnr"
  681. #define kOTPseudoPortScannerInterfaceID        kOTKernelPrefix "ppScnr"
  682. #define kOTCompatScannerInterfaceID            kOTKernelPrefix "cpScnr,1.0"
  683.  
  684. #define kOTPortScannerCFMTag                kOTKernelPrefix "pScnr"
  685. #define kOTPseudoPortScannerCFMTag            kOTKernelPrefix "ppScnr"
  686. #define kOTCompatPortScannerCFMTag            kOTKernelPrefix "cpScnr"
  687.  
  688.  
  689. /* OTScanPorts entry point.*/
  690.  
  691. /* Your port-scanning function must be exported by the name "OTScanPorts".*/
  692.  
  693. #define kOTScanPortsID "OTScanPorts"
  694. /* Selectors for the scanType parameter to PortScanProcPtr.*/
  695.  
  696. enum {
  697.     kOTInitialScan                = 0,
  698.     kOTScanAfterSleep            = 1
  699. };
  700.  
  701. /* PortScanProcPtr is the typedef for the scanning function.*/
  702.  
  703. typedef CALLBACK_API_C( void , PortScanProcPtr )(UInt32 scanType);
  704. /*
  705.    Memory allocation for port persistent data, such as the
  706.    memory referenced by the ref parameter you pass to
  707.    OTRegisterPort.
  708. */
  709. EXTERN_API_C( void *)
  710. OTAllocPortMem                    (OTByteCount             size);
  711.  
  712. EXTERN_API_C( void )
  713. OTFreePortMem                    (void *                    mem);
  714.  
  715. /* ***** Interrupt Control Functions *****/
  716.  
  717. #if TARGET_CPU_68K
  718. /*
  719.    MPS_INTR_STATE saves the current interrupt state
  720.    Its definition changes with the definition
  721.    of mps_intr_enable/disable, so this typedef is
  722.    also inside the TARGET_CPU_68K conditional.
  723. */
  724.  
  725. typedef UInt8                             MPS_INTR_STATE;
  726. /* Disable interrupts and save the state*/
  727.                                                                                             #if TARGET_OS_MAC && TARGET_CPU_68K && !TARGET_RT_MAC_CFM
  728.                                                                                             #pragma parameter mps_intr_disable(__A0)
  729.                                                                                             #endif
  730. EXTERN_API_C( void )
  731. mps_intr_disable                (MPS_INTR_STATE *        oldState)                            SEVENWORDINLINE(0x40C0, 0xE040, 0x007C, 0x0600, 0x027C, 0xFEFF, 0x1080);
  732.  
  733. /*
  734.    move sr,d0
  735.    asr.w #8,d0
  736.    ori #$600,sr
  737.    andi #$FEFF,sr
  738.    move.b d0,(a0)
  739.    #if OTDEBUG
  740.        andi.b #$70,d0
  741.        cmpi.b #$70,d0
  742.        bne.s @3
  743.        DebugBreak
  744.    #endif
  745. */
  746.  
  747. /* Enable interrupts from the saved state*/
  748.  
  749.                                                                                             #if TARGET_OS_MAC && TARGET_CPU_68K && !TARGET_RT_MAC_CFM
  750.                                                                                             #pragma parameter mps_intr_enable(__A0)
  751.                                                                                             #endif
  752. EXTERN_API_C( void )
  753. mps_intr_enable                    (MPS_INTR_STATE *        oldState)                            THREEWORDINLINE(0x1010, 0xE140, 0x46C0);
  754.  
  755. /*
  756.    #if OTDEBUG
  757.        move    sr,d0
  758.        andi.w #$700,d0
  759.        cmpi.w #$600,d0
  760.        beq.s @2
  761.        DebugBreak
  762.    #endif
  763.    move.b (a0),d0
  764.    #if OTDEBUG
  765.        andi.b #$70,d0
  766.        cmpi.b #$70,d0
  767.        bne.s @3
  768.        DebugBreak
  769.        move.b (a0),d0
  770.    #endif
  771.    asl.w #8,d0
  772.    move    d0,sr
  773. */
  774.  
  775. #else
  776. typedef UInt8                             MPS_INTR_STATE;
  777. EXTERN_API_C( void )
  778. mps_intr_disable                (MPS_INTR_STATE *        oldState);
  779.  
  780. EXTERN_API_C( void )
  781. mps_intr_enable                    (MPS_INTR_STATE *        oldState);
  782.  
  783. #endif  /* TARGET_CPU_68K */
  784.  
  785. /* ***** Timer Functions ******/
  786. enum {
  787.     kOTMinimumTimerValue        = 8                                /* 8 milliseconds is the minimum timeout value*/
  788. };
  789.  
  790. EXTERN_API_C( mblk_t *)
  791. mi_timer_alloc                    (queue_t *                q,
  792.                                  OTByteCount             size);
  793.  
  794. EXTERN_API_C( void )
  795. mi_timer_free                    (mblk_t *                mp);
  796.  
  797. EXTERN_API_C( void )
  798. mi_timer                        (mblk_t *                mp,
  799.                                  unsigned long             milliSeconds);
  800.  
  801. EXTERN_API_C( Boolean )
  802. mi_timer_cancel                    (mblk_t *                mp);
  803.  
  804. EXTERN_API_C( Boolean )
  805. mi_timer_valid                    (mblk_t *                mp);
  806.  
  807. EXTERN_API_C( mblk_t *)
  808. mi_timer_q_switch                (mblk_t *                mp,
  809.                                  queue_t *                q,
  810.                                  mblk_t *                new_mp);
  811.  
  812. /* ***** Driver Deferred Task Extras ******/
  813.  
  814. /*
  815.    This routine is used by a driver at interrupt time to schedule
  816.    a deferred task to run their interrupt processing code.
  817. */
  818. EXTERN_API_C( Boolean )
  819. OTScheduleDriverDeferredTask    (long                     dtCookie);
  820.  
  821. /* ***** Driver Memory Routines ******/
  822.  
  823. /*
  824.    These routines are different from the
  825.    similarly named routines in "OpenTransport.h" because they allocate
  826.    memory in the OT kernel pool.  See Technote oooo "Understanding
  827.    Open Transport Memory Management" for details.
  828. */
  829.  
  830. /*
  831.    This is the typedef for a function that will be called when a message
  832.    created by OTAllocMsg is destroyed.
  833. */
  834.  
  835. typedef CALLBACK_API_C( void , EsbFreeProcPtr )(char *arg);
  836. /*
  837.    This function creates a message which points to "size" bytes of data
  838.    at "buf".  When the message is freed, the EsbFreeProcPtr function "func"
  839.    will be called with the argument "arg".
  840.    NOTE: This function allows users of your buffer to modify the buffer.
  841. */
  842. EXTERN_API_C( mblk_t *)
  843. OTAllocMsg                        (void *                    buf,
  844.                                  OTByteCount             size,
  845.                                  EsbFreeProcPtr         func,
  846.                                  void *                    arg);
  847.  
  848. /*
  849.    Routines to allocate and free memory in your modules (these are
  850.    interrupt-time safe!).
  851. */
  852.  
  853. EXTERN_API_C( void *)
  854. OTAllocMem                        (OTByteCount             size);
  855.  
  856. EXTERN_API_C( void )
  857. OTFreeMem                        (void *                    mem);
  858.  
  859. EXTERN_API_C( void *)
  860. OTReallocMem                    (void *                    ptr,
  861.                                  OTByteCount             newSize);
  862.  
  863. /*
  864.    Also, make sure that calling traditional "C" allocation routines
  865.    will not compile.
  866. */
  867.  
  868.  
  869. #define calloc                        (DONT_CALL*THIS_FUNCTION)
  870. #define malloc                        (DONT_CALL*THIS_FUNCTION)
  871. #define realloc                        (DONT_CALL*THIS_FUNCTION)
  872. #define free                        (DONT_CALL*THIS_FUNCTION)
  873.  
  874. /* ***** Kernel Memory Utilities ******/
  875.  
  876. EXTERN_API_C( OTInt32 )
  877. mi_bcmp                            (const char *            first,
  878.                                  const char *            second,
  879.                                  OTByteCount             nBytes);
  880.  
  881. /* Standard STREAMS memory utilities.*/
  882.  
  883.     
  884. #define bcopy(s, d, l)            OTMemcpy(d, s, l)
  885. #define bzero(d, l)                OTMemzero(d, (OTByteCount)(l))
  886. #define bcmp(s, d, l)            mi_bcmp(s, d, l)
  887.  
  888. /*
  889.    Standard STREAMS bcopy, bzero, & bcmp take char* parameters.
  890.    The BCOPY, BZERO, and BCMP routines take void* so that we do not have
  891.    to cast all the pointers.
  892. */
  893.  
  894.  
  895. #define BCOPY(s, d, l)            bcopy((const char*)(s), (char*)(d), l)
  896. #define BZERO(d, l)                bzero((char*)(d), l)
  897. #define BCMP(s, d, l)            mi_bcmp((const char*)(s), (const char*)(d), l)
  898.  
  899.  
  900. /*******************************************************************************
  901. ** Some helpful utilites from Mentat
  902. ********************************************************************************/
  903. /* Routines to calculate various sizes of STREAM messages*/
  904.  
  905.  
  906. #define HEAD_SIZE(mp)    ((mp)->b_rptr - (mp)->b_datap->db_base)
  907. #define TAIL_SIZE(mp)    ((mp)->b_datap->db_lim - (mp)->b_wptr)
  908. #define MBLK_SIZE(mp)    ((mp)->b_wptr - (mp)->b_rptr)
  909. #define DBLK_SIZE(mp)    ((mp)->b_datap->db_lim - (mp)->b_datap->db_base)
  910.  
  911. /* Useful macros for STREAMS copy in and out.*/
  912.  
  913.  
  914. #define    MI_COPY_IN        1
  915. #define    MI_COPY_OUT        2
  916. #define    MI_COPY_DIRECTION(mp)                             \
  917.     (((UInt8*)&(mp)->b_cont->b_prev)[0])
  918. #define    MI_COPY_COUNT(mp)                     \
  919.     (((UInt8*)&(mp)->b_cont->b_prev)[1])
  920. #define    MI_COPY_RVAL(mp)    (*(OTInt32 *)&(mp)->b_cont->b_next)
  921. #define    MI_COPY_CASE(dir,cnt)    ((OTInt32)(((cnt)<<2)|dir))
  922. #define    MI_COPY_STATE(mp)                             \
  923.     ((OTInt32)MI_COPY_CASE(MI_COPY_DIRECTION(mp),MI_COPY_COUNT(mp)))
  924.  
  925. typedef CALLBACK_API_C( void , OTWriterProcPtr )(queue_t *q, mblk_t *mp);
  926. EXTERN_API_C( void )
  927. mps_become_writer                (queue_t *                q,
  928.                                  mblk_t *                mp,
  929.                                  OTWriterProcPtr         proc);
  930.  
  931. EXTERN_API_C( OTInt32 )
  932. drv_priv                        (cred *                    credp);
  933.  
  934. EXTERN_API_C( queue_t *)
  935. mi_allocq                        (streamtab *            st);
  936.  
  937. EXTERN_API_C( mblk_t *)
  938. mi_tpi_ack_alloc                (mblk_t *                mp,
  939.                                  OTByteCount             size,
  940.                                  long                     primType);
  941.  
  942. EXTERN_API_C( mblk_t *)
  943. mi_tpi_conn_con                    (mblk_t *                trailer_mp,
  944.                                  char *                    src,
  945.                                  OTByteCount             src_length,
  946.                                  char *                    opt,
  947.                                  OTByteCount             opt_length);
  948.  
  949. EXTERN_API_C( mblk_t *)
  950. mi_tpi_conn_ind                    (mblk_t *                trailer_mp,
  951.                                  char *                    src,
  952.                                  OTByteCount             src_length,
  953.                                  char *                    opt,
  954.                                  OTByteCount             opt_length,
  955.                                  OTInt32                 seqnum);
  956.  
  957. EXTERN_API_C( mblk_t *)
  958. mi_tpi_conn_req                    (mblk_t *                trailer_mp,
  959.                                  char *                    dest,
  960.                                  OTByteCount             dest_length,
  961.                                  char *                    opt,
  962.                                  OTByteCount             opt_length);
  963.  
  964. EXTERN_API_C( mblk_t *)
  965. mi_tpi_data_ind                    (mblk_t *                trailer_mp,
  966.                                  OTInt32                 flags,
  967.                                  long                     ptype);
  968.  
  969. EXTERN_API_C( mblk_t *)
  970. mi_tpi_data_req                    (mblk_t *                trailer_mp,
  971.                                  OTInt32                 flags,
  972.                                  long                     pttype);
  973.  
  974. EXTERN_API_C( mblk_t *)
  975. mi_tpi_discon_ind                (mblk_t *                trailer_mp,
  976.                                  OTInt32                 reason,
  977.                                  OTInt32                 seqnum);
  978.  
  979. EXTERN_API_C( mblk_t *)
  980. mi_tpi_discon_req                (mblk_t *                trailer_mp,
  981.                                  OTInt32                 seqnum);
  982.  
  983. EXTERN_API_C( mblk_t *)
  984. mi_tpi_err_ack_alloc            (mblk_t *                mp,
  985.                                  OTInt32                 tlierr,
  986.                                  OTInt32                 unixerr);
  987.  
  988. EXTERN_API_C( mblk_t *)
  989. mi_tpi_exdata_ind                (mblk_t *                trailer_mp,
  990.                                  OTInt32                 flags,
  991.                                  long                     ptype);
  992.  
  993. EXTERN_API_C( mblk_t *)
  994. mi_tpi_exdata_req                (mblk_t *                trailer_mp,
  995.                                  OTInt32                 flags,
  996.                                  long                     ptype);
  997.  
  998. EXTERN_API_C( mblk_t *)
  999. mi_tpi_info_req                    (void);
  1000.  
  1001. EXTERN_API_C( mblk_t *)
  1002. mi_tpi_ok_ack_alloc                (mblk_t *                mp);
  1003.  
  1004. EXTERN_API_C( mblk_t *)
  1005. mi_tpi_ordrel_ind                (void);
  1006.  
  1007. EXTERN_API_C( mblk_t *)
  1008. mi_tpi_ordrel_req                (void);
  1009.  
  1010. EXTERN_API_C( mblk_t *)
  1011. mi_tpi_uderror_ind                (char *                    dest,
  1012.                                  OTByteCount             dest_length,
  1013.                                  char *                    opt,
  1014.                                  OTByteCount             opt_length,
  1015.                                  OTInt32                 error);
  1016.  
  1017. EXTERN_API_C( mblk_t *)
  1018. mi_tpi_unitdata_ind                (mblk_t *                trailer_mp,
  1019.                                  char *                    src,
  1020.                                  OTByteCount             src_length,
  1021.                                  char *                    opt,
  1022.                                  OTByteCount             opt_length);
  1023.  
  1024. EXTERN_API_C( mblk_t *)
  1025. mi_tpi_unitdata_req                (mblk_t *                trailer_mp,
  1026.                                  char *                    dst,
  1027.                                  OTByteCount             dst_length,
  1028.                                  char *                    opt,
  1029.                                  OTByteCount             opt_length);
  1030.  
  1031. EXTERN_API_C( mblk_t *)
  1032. mi_reuse_proto                    (mblk_t *                toReuse,
  1033.                                  OTByteCount             sizeDesired,
  1034.                                  OTBooleanParam         keepOnError);
  1035.  
  1036. EXTERN_API_C( mblk_t *)
  1037. mi_reallocb                        (mblk_t *                old_mp,
  1038.                                  OTByteCount             new_size);
  1039.  
  1040. EXTERN_API_C( Boolean )
  1041. mi_set_sth_hiwat                (queue_t *                q,
  1042.                                  OTByteCount             size);
  1043.  
  1044. EXTERN_API_C( Boolean )
  1045. mi_set_sth_lowat                (queue_t *                q,
  1046.                                  OTByteCount             size);
  1047.  
  1048. EXTERN_API_C( Boolean )
  1049. mi_set_sth_maxblk                (queue_t *                q,
  1050.                                  OTByteCount             size);
  1051.  
  1052. EXTERN_API_C( Boolean )
  1053. mi_set_sth_wroff                (queue_t *                q,
  1054.                                  OTByteCount             size);
  1055.  
  1056. EXTERN_API_C( UInt8 *)
  1057. mi_offset_param                    (mblk_t *                mp,
  1058.                                  long                     offset,
  1059.                                  long                     len);
  1060.  
  1061. EXTERN_API_C( UInt8 *)
  1062. mi_offset_paramc                (mblk_t *                mp,
  1063.                                  long                     offset,
  1064.                                  long                     len);
  1065.  
  1066. EXTERN_API_C( char *)
  1067. mi_open_detached                (char **                mi_opp_orig,
  1068.                                  OTByteCount             size,
  1069.                                  dev_t *                devp);
  1070.  
  1071. EXTERN_API_C( OTInt32 )
  1072. mi_open_comm                    (char **                mi_opp_orig,
  1073.                                  OTByteCount             size,
  1074.                                  queue_t *                q,
  1075.                                  dev_t *                dev,
  1076.                                  OTInt32                 flag,
  1077.                                  OTInt32                 sflag,
  1078.                                  cred_t *                credp);
  1079.  
  1080. EXTERN_API_C( OTInt32 )
  1081. mi_close_comm                    (char **                mi_opp_orig,
  1082.                                  queue_t *                q);
  1083.  
  1084. EXTERN_API_C( void )
  1085. mi_bufcall                        (queue_t *                q,
  1086.                                  OTByteCount             size,
  1087.                                  OTInt32                 pri);
  1088.  
  1089. EXTERN_API_C( void )
  1090. mi_detach                        (queue_t *                q,
  1091.                                  char *                    ptr);
  1092.  
  1093. EXTERN_API_C( void )
  1094. mi_close_detached                (char **                mi_opp_orig,
  1095.                                  char *                    ptr);
  1096.  
  1097. EXTERN_API_C( char *)
  1098. mi_next_ptr                        (char *                    ptr);
  1099.  
  1100. EXTERN_API_C( void )
  1101. mi_copyin                        (queue_t *                q,
  1102.                                  mblk_t *                mp,
  1103.                                  char *                    uaddr,
  1104.                                  OTByteCount             len);
  1105.  
  1106. EXTERN_API_C( void )
  1107. mi_copyout                        (queue_t *                q,
  1108.                                  mblk_t *                mp);
  1109.  
  1110. EXTERN_API_C( mblk_t *)
  1111. mi_copyout_alloc                (queue_t *                q,
  1112.                                  mblk_t *                mp,
  1113.                                  char *                    uaddr,
  1114.                                  OTByteCount             len);
  1115.  
  1116. EXTERN_API_C( void )
  1117. mi_copy_done                    (queue_t *                q,
  1118.                                  mblk_t *                mp,
  1119.                                  OTInt32                 err);
  1120.  
  1121. EXTERN_API_C( void )
  1122. mi_copy_set_rval                (mblk_t *                mp,
  1123.                                  OTInt32                 rval);
  1124.  
  1125. EXTERN_API_C( OTInt32 )
  1126. mi_copy_state                    (queue_t *                q,
  1127.                                  mblk_t *                mp,
  1128.                                  mblk_t **                mpp);
  1129.  
  1130. /* ***** PCI-Specific Stuff*/
  1131.  
  1132. /* This is the cookie that is passed to your STREAM Module.*/
  1133.  
  1134.  
  1135. struct OTPCIInfo {
  1136.     RegEntryID                         fTheID;
  1137.     void *                            fConfigurationInfo;
  1138.     ByteCount                         fConfigurationLength;
  1139. };
  1140. typedef struct OTPCIInfo                OTPCIInfo;
  1141. /* Driver Description*/
  1142. /*
  1143.    All PCI card drivers should have the following bits set in their
  1144.    install_info structure:
  1145.    
  1146.    o kOTModIsDriver.
  1147.    
  1148.    They should NEVER have the kOTModPushAlways or the kOTModIsModule
  1149.    flags set.
  1150.    The kOTModIsLowerMux bits should be set if the driver is a lower
  1151.    multiplexor, although Open Transport does nothing with the information
  1152.    today.
  1153.    The kOTModUpperIsDLPI bit should be set if the driver uses the DLPI message
  1154.    specification.  The kOTModUpperIsTPI bit should be set if the driver uses
  1155.    the TPI message specification.
  1156. */
  1157.  
  1158. /*
  1159.    Macro to put together the driverServices.service[x].serviceType field:
  1160.    xxxxxddd dddddddd ffffffff xxxxxxTD
  1161.    where "d" is the device type for Open Transport,
  1162.    the lower two bits are whether the driver is TPI or DLPI,
  1163.    and the "f" bits are the framing option flags.
  1164.    and all other bits should be 0
  1165. */
  1166.  
  1167.  
  1168. #define OTPCIServiceType(devType, framingFlags, isTPI, isDLPI)    \
  1169.     ((devType << 16) | (((framingFlags) & 0xff) << 8) | (isTPI ? 2 : 0) | (isDLPI ? 1 : 0 ))
  1170.  
  1171. /*
  1172.    Typedef for the ValidateHardware function.  This function will be
  1173.    called only once, at system boot time, before installing your driver
  1174.    into the Open Transport module registry.
  1175.    The param pointer will is a OTPCIInfo pointer - don't be changing the
  1176.    values there!
  1177. */
  1178.  
  1179. typedef CALLBACK_API_C( OTResult , ValidateHardwareProcPtr )(OTPCIInfo *param);
  1180. /*
  1181.    Your driver can return this value if it loaded correctly
  1182.    but wants to stay resident, presumably because it's hooked
  1183.    itself irrevokably into some other system service.
  1184. */
  1185. enum {
  1186.     kOTPCINoErrorStayLoaded        = 1
  1187. };
  1188.  
  1189. /*
  1190.    Some descriptors we use - these should eventually show up
  1191.    in system header files somewhere.
  1192. */
  1193.  
  1194.  
  1195. #define kDescriptorProperty        "driver-descriptor"
  1196. #define kDriverProperty            "driver,AAPL,MacOS,PowerPC"
  1197. #define kDriverPtrProperty        "driver-ptr"
  1198. #define kSlotProperty            "AAPL,slot-name"
  1199.  
  1200. /*
  1201.    Maximum # of services support by Open Transport.  If your module
  1202.    exports more than this # of services, Open Transport will not be
  1203.    able to use the module.
  1204. */
  1205.  
  1206.  
  1207. enum {
  1208.     kMaxServices                = 20
  1209. };
  1210.  
  1211. /*******************************************************************************
  1212. ** Everything below here is C++ ONLY
  1213. ********************************************************************************/
  1214.  
  1215. #ifdef __cplusplus
  1216. }                    // Terminate C definitions
  1217.  
  1218. /*    -------------------------------------------------------------------------
  1219.     TStreamQueue class
  1220.     
  1221.     This class is just a convenient interface to the queue structure
  1222.     ------------------------------------------------------------------------- */
  1223.  
  1224.     class TStreamQueue : public queue
  1225.     {
  1226.         public:
  1227.             void        EnableQueue();
  1228.     };
  1229.     
  1230.     /*    -------------------------------------------------------------------------
  1231.         Inline methods for TStreamQueue
  1232.         ------------------------------------------------------------------------- */
  1233.     
  1234.         inline void TStreamQueue::EnableQueue()
  1235.         {
  1236.             if ( q_flag & QNOENB )
  1237.             {
  1238.                 enableok(this);
  1239.                 if ( q_first )
  1240.                     qenable(this);
  1241.             }
  1242.         }
  1243.  
  1244. /*    -------------------------------------------------------------------------
  1245.     Class TStreamMessage
  1246.     
  1247.     This class is a C++ interface to the mblk_t structure defined in STREAMS
  1248.     ------------------------------------------------------------------------- */
  1249.     //
  1250.     // The maximum stream buffer size is the largest OTByteCount that
  1251.     // doesn't look negative if interpreted as a signed number
  1252.     //
  1253.     enum
  1254.     {
  1255.         kMaxStreamBufferSize    = (((OTByteCount)-1L) >> 1)
  1256.     };
  1257.  
  1258.     class TStreamMessage : public msgb
  1259.     {
  1260.         private:
  1261.                 void*        operator new (OTByteCount)    { return NULL; }
  1262.                 
  1263.         public:
  1264.                 void*        operator new(OTByteCount, OTByteCount size)
  1265.                     { return (TStreamMessage*)allocb(size, 0); }
  1266.                 void*        operator new(OTByteCount, void* buf, OTByteCount size,
  1267.                                          EsbFreeProcPtr func, void* arg)
  1268.                     { return OTAllocMsg(buf, size, func, arg); }
  1269.                 void        operator delete(void* ptr)
  1270.                     { if ( ptr != NULL ) freemsg((mblk_t*)ptr); }
  1271.     
  1272.                     void            Reset(OTByteCount = 0);
  1273.                     void            ResetWithLeader(OTByteCount);
  1274.             
  1275.                     void            FreeData();
  1276.                     TStreamMessage*    RemoveData();
  1277.                     void            AppendData(TStreamMessage* data);
  1278.             
  1279.                     OTByteCount    GetSize() const;
  1280.                     OTByteCount    GetDataSize() const;
  1281.                     OTByteCount    GetMessageDataSize() const;
  1282.                     void                SetDataSize(OTByteCount);
  1283.         
  1284.                     void            SetType(UInt8);
  1285.                     UInt8            GetType() const;
  1286.                     TStreamMessage*    GetNextBlock() const;
  1287.                     TStreamMessage*    ReuseMessage(OTByteCount newSize, OTBooleanParam keepOnFailure);
  1288.                     
  1289.                     void            SetNextBlock(TStreamMessage*);
  1290.                     char*            GetDataPointer() const;
  1291.                     Boolean            IsReuseable(OTByteCount size) const;
  1292.             
  1293.                     void            HideBytesAtFront(OTByteCount);
  1294.                     void            HideBytesAtEnd(OTByteCount);
  1295.             //
  1296.             // The following functions ignore non-M_DATA blocks.
  1297.             //
  1298.                     const TStreamMessage*
  1299.                                     _MDECL GetBlockAt(OTByteCount& offset) const;
  1300.                     const void*        _MDECL GetPointerTo(OTByteCount offset, OTByteCount* len) const;
  1301.                     const void*        _MDECL GetPointerTo(OTByteCount offset, OTByteCount* len, void* bfr) const;
  1302.             //
  1303.             // This function makes a non-shared copy of any initial non-M_DATA block, and
  1304.             // then makes shared copies of the requested data.  It only assumes 1 leading
  1305.             // non-M_DATA block, but will skip any other non-M_DATA blocks while 
  1306.             // searching for data to copy.
  1307.             //
  1308.                     TStreamMessage*    _MDECL MakeSharedCopy(OTByteCount offset = 0, 
  1309.                                                           OTByteCount len = kMaxStreamBufferSize);
  1310.  
  1311.                     Boolean            _MDECL HasData() const;
  1312.                     Boolean            _MDECL WriteData(void* buf, OTByteCount len);
  1313.             //
  1314.             // These hide/remove the part of the data already read.  
  1315.             //
  1316.                     TStreamMessage*    _MDECL ReadData(void* buf, OTByteCount* len);
  1317.                     OTByteCount        _MDECL ReadControl(void* buf, OTByteCount len);
  1318.     };
  1319.     
  1320.     /*    -------------------------------------------------------------------------
  1321.         Inline methods for TStreamMessage
  1322.         ------------------------------------------------------------------------- */
  1323.     
  1324.         inline void TStreamMessage::Reset(OTByteCount size)
  1325.         {
  1326.             b_rptr = b_datap->db_base;
  1327.             b_wptr = b_rptr + size;
  1328.         }
  1329.         
  1330.         inline OTByteCount OTLengthWithLeader(OTByteCount size)
  1331.         {
  1332.             return (size + 3) & ~3;
  1333.         }
  1334.         
  1335.         inline void TStreamMessage::ResetWithLeader(OTByteCount size)
  1336.         {
  1337.             b_rptr = b_datap->db_lim - OTLengthWithLeader(size);
  1338.             b_wptr = b_rptr + size;
  1339.         }
  1340.         
  1341.         inline void TStreamMessage::FreeData()
  1342.         {
  1343.             if ( b_cont )
  1344.             {
  1345.                 freemsg(b_cont);
  1346.                 b_cont = NULL;
  1347.             }
  1348.         }
  1349.         
  1350.         inline TStreamMessage* TStreamMessage::RemoveData()
  1351.         {
  1352.             TStreamMessage* temp = (TStreamMessage*)b_cont;
  1353.             b_cont = NULL;
  1354.             return temp;
  1355.         }
  1356.         
  1357.         inline void TStreamMessage::AppendData(TStreamMessage* mp)
  1358.         {
  1359.             linkb(this, mp);
  1360.         }
  1361.     
  1362.         inline OTByteCount TStreamMessage::GetSize() const
  1363.         {
  1364.             return b_datap->db_lim - b_datap->db_base;
  1365.         }
  1366.         
  1367.         inline OTByteCount TStreamMessage::GetDataSize() const
  1368.         {
  1369.             return b_wptr - b_rptr;
  1370.         }
  1371.         
  1372.         inline OTByteCount TStreamMessage::GetMessageDataSize() const
  1373.         {
  1374.             return (b_cont == NULL) ? 
  1375.                 (b_datap->db_type == M_DATA ? (b_wptr - b_rptr) : 0) : msgdsize(this);
  1376.         }
  1377.     
  1378.         inline void TStreamMessage::SetDataSize(OTByteCount size)
  1379.         {
  1380.             b_wptr = b_rptr + size;
  1381.         }
  1382.     
  1383.         inline void TStreamMessage::SetType(UInt8 type)
  1384.         {
  1385.             b_datap->db_type = type;
  1386.         }
  1387.     
  1388.         inline unsigned char TStreamMessage::GetType() const
  1389.         {
  1390.             return b_datap->db_type;
  1391.         }
  1392.         
  1393.         inline TStreamMessage* TStreamMessage::GetNextBlock() const
  1394.         {
  1395.             return (TStreamMessage*)b_cont;
  1396.         }
  1397.         
  1398.         inline void TStreamMessage::SetNextBlock(TStreamMessage* mp)
  1399.         {
  1400.             b_cont = (mblk_t*)mp;
  1401.         }
  1402.         
  1403.         inline char* TStreamMessage::GetDataPointer() const
  1404.         {
  1405.             return (char*)b_rptr;
  1406.         }
  1407.         
  1408.         inline Boolean TStreamMessage::IsReuseable(OTByteCount size) const
  1409.         {
  1410.             return (b_datap->db_ref == 1 && GetSize() >= size);
  1411.         }
  1412.         
  1413.         inline void TStreamMessage::HideBytesAtFront(OTByteCount len)
  1414.         {
  1415.             adjmsg(this, len);
  1416.         }
  1417.         
  1418.         inline void TStreamMessage::HideBytesAtEnd(OTByteCount len)
  1419.         {
  1420.             adjmsg(this, -len);
  1421.         }
  1422.  
  1423.         inline TStreamMessage* TStreamMessage::ReuseMessage(OTByteCount newSize, OTBooleanParam keepOnFailure)
  1424.         {
  1425.             return (TStreamMessage*)mi_reuse_proto(this, newSize, keepOnFailure);
  1426.         }
  1427.         
  1428. /*    -------------------------------------------------------------------------
  1429.     TTimerMessage class
  1430.     
  1431.     This class implements an interface to the STREAM environment timer
  1432.     facilities.  A TTimerMessage will be placed on the queue of your choice
  1433.     when it's timer expires.
  1434.     ------------------------------------------------------------------------- */
  1435.  
  1436. #if TARGET_CPU_PPC
  1437.     #define TIMER_BUG    1
  1438. #else
  1439.     #define TIMER_BUG    0
  1440. #endif
  1441.  
  1442.     class TTimerMessage : public TStreamMessage
  1443.     {
  1444.     #if TIMER_BUG
  1445.         private:
  1446.     #else
  1447.         public:
  1448.     #endif
  1449.             inline void* operator new(OTByteCount, queue_t* q)
  1450.                 {    return mi_timer_alloc(q, sizeof(T_stream_timer)); }
  1451.             inline void* operator new(OTByteCount, queue_t* q, OTByteCount extra)
  1452.                 {    return mi_timer_alloc(q, extra + sizeof(T_stream_timer)); }
  1453.     
  1454.             inline void operator delete(void* ptr)
  1455.                 {    if ( ptr != NULL ) mi_timer_free((mblk_t*)ptr); }
  1456.     
  1457.         public:        
  1458.                     Boolean            IsValid();
  1459.                     TTimerMessage*    ChangeQueue(TStreamQueue* newQ, TTimerMessage* newMP);
  1460.                     Boolean            Cancel();
  1461.                     void            Schedule(OTTimeout time);
  1462.     
  1463.         private:
  1464.             inline void* operator new(OTByteCount)
  1465.                 {    return 0; }
  1466.     };
  1467.     
  1468.     inline TTimerMessage* NewTimerMsg(queue_t* q, OTByteCount extra = 0)
  1469.     {
  1470.         return (TTimerMessage*)mi_timer_alloc(q, sizeof(T_stream_timer) + extra);
  1471.     }
  1472.     
  1473.     inline void FreeTimerMsg(TTimerMessage* msg)
  1474.     {
  1475.         mi_timer_free(msg);
  1476.     }
  1477.     
  1478.     /*    -------------------------------------------------------------------------
  1479.         Inline methods for TStreamTimer
  1480.         ------------------------------------------------------------------------- */
  1481.     
  1482.         inline Boolean TTimerMessage::IsValid()
  1483.         {
  1484.             return mi_timer_valid(this);
  1485.         }
  1486.         
  1487.         inline TTimerMessage* TTimerMessage::ChangeQueue(TStreamQueue* q, TTimerMessage* newMP)
  1488.         {
  1489.             return( (TTimerMessage*) mi_timer_q_switch(this, q, newMP));
  1490.         }
  1491.     
  1492.         inline Boolean TTimerMessage::Cancel()
  1493.         {
  1494.             return mi_timer_cancel(this);
  1495.         }
  1496.         
  1497.         inline void TTimerMessage::Schedule(OTTimeout time)
  1498.         {
  1499.             ((T_stream_timer*)b_rptr)->PRIM_type = T_TIMER_REQ;
  1500.             mi_timer(this, (unsigned long)time);
  1501.         }
  1502.  
  1503. extern "C" {        // resume C definitions
  1504. #endif    /* __cplusplus    */
  1505.  
  1506.  
  1507. #if defined(__MWERKS__) && TARGET_CPU_68K
  1508.     #pragma pop
  1509. #endif
  1510.  
  1511.  
  1512. #if PRAGMA_STRUCT_ALIGN
  1513.     #pragma options align=reset
  1514. #elif PRAGMA_STRUCT_PACKPUSH
  1515.     #pragma pack(pop)
  1516. #elif PRAGMA_STRUCT_PACK
  1517.     #pragma pack()
  1518. #endif
  1519.  
  1520. #ifdef PRAGMA_IMPORT_OFF
  1521. #pragma import off
  1522. #elif PRAGMA_IMPORT
  1523. #pragma import reset
  1524. #endif
  1525.  
  1526. #ifdef __cplusplus
  1527. }
  1528. #endif
  1529.  
  1530. #endif /* __OPENTRANSPORTKERNEL__ */
  1531.  
  1532.